package com.yxkj.syh.app.huarong.views;

import android.app.Dialog;
import android.content.Context;
import android.text.TextUtils;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.bumptech.glide.load.engine.DiskCacheStrategy;
import com.bumptech.glide.request.RequestOptions;
import com.syh.app.basic.utils.AppUtil;
import com.syh.app.basic.utils.ComUtil;
import com.syh.app.basic.utils.Tooast;
import com.syh.app.basic.views.ImageWatcher;
import com.yxkj.syh.app.huarong.R;
import com.yxkj.syh.app.huarong.activities.creat.edit.EditOrderActivity;

import java.util.ArrayList;
import java.util.List;

/**
 * 创建者：SYH
 * 时间：2020/6/12 17:51
 * 编辑者：SYH、
 * 标签：
 * 补充说明：
 */
public class FinishOrderDialog extends Dialog {
    private Listener mListener;

    private WindowManager.LayoutParams mLayoutParams;
    private Context mContext;
    private TextView tvCancel;
    private Button btnCommit;
    private ImageView ivAddShds;
    private LinearLayout llShds;
    private List<String> shdsUrlList = new ArrayList<>();

    public FinishOrderDialog(Context context, Listener listener) {
        super(context, R.style.FullDialog);
        initContentView();
        init(context);
        this.mListener = listener;
    }

    private void init(Context context) {
        this.mContext = context;
        Window dialogWindow = this.getWindow();
        mLayoutParams = dialogWindow.getAttributes();
        mLayoutParams.width = AppUtil.getScreenWidth(context);
        dialogWindow.setAttributes(mLayoutParams);
        dialogWindow.getDecorView().setPadding(0, 0, 0, 0);
        dialogWindow.getDecorView().setBackgroundResource(android.R.color.transparent);
        dialogWindow.setGravity(Gravity.BOTTOM);
    }

    private void initContentView() {
        View dialogView = LayoutInflater.from(this.getContext()).inflate(R.layout.dialog_finish_order, (ViewGroup) null);

        tvCancel = dialogView.findViewById(R.id.tvCancel);
        llShds = dialogView.findViewById(R.id.llShds);
        btnCommit = dialogView.findViewById(R.id.btnCommit);
        ivAddShds = dialogView.findViewById(R.id.ivAddShds);

//        ivRemoveShds.setOnClickListener(v -> {
//            ivRemoveShds.setVisibility(View.INVISIBLE);
//            ivEditShds.setImageResource(R.mipmap.ic_add_image);
//            imageUrl = "";
//        });

        ivAddShds.setOnClickListener(v -> {
            mListener.onUpload();
        });

        tvCancel.setOnClickListener(v -> cancel());
        btnCommit.setOnClickListener(v -> {
            if (shdsUrlList.size() == 0) {
                Tooast.normalInfo("请点击上传送货单");
            } else {
                mListener.onCommit(shdsUrlList);
            }
        });
        setContentView(dialogView);
    }

    public void setImageUrl(String imageUrl) {
        addShdfView(imageUrl);
    }

    /**
     * 添加送货单(发)图片
     *
     * @param url
     */
    private void addShdfView(String url) {
        LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(ComUtil.Dp2Px(mContext, 90), ComUtil.Dp2Px(mContext, 90));
        layoutParams.rightMargin = ComUtil.Dp2Px(mContext, 10);

        shdsUrlList.add(url);
        FrameLayout pzLayout = (FrameLayout) LayoutInflater.from(mContext).inflate(R.layout.activity_shdf, null);
        ImageView ivEditPz = pzLayout.findViewById(R.id.ivEditPz);
        ImageView ivRemovePz = pzLayout.findViewById(R.id.ivRemovePz);
        Glide.with(mContext).load(url).apply(RequestOptions.errorOf(R.mipmap.ic_add_image).diskCacheStrategy(DiskCacheStrategy.NONE)).into(ivEditPz);
        ivEditPz.setTag(url);
        ivRemovePz.setTag(url);
        ivEditPz.setOnClickListener(v -> {
            new ImageWatcher(mContext).show((String) v.getTag());
        });
        ivRemovePz.setOnClickListener(v -> {
            shdsUrlList.remove(v.getTag());
            llShds.removeView(pzLayout);
            llShds.requestLayout();
            setAddPzVisibility();
        });

        setAddPzVisibility();

        llShds.addView(pzLayout, layoutParams);
        pzLayout.requestLayout();
    }


    private void setAddPzVisibility() {
        if (shdsUrlList.size() < 6) {
            ivAddShds.setVisibility(View.VISIBLE);
        } else {
            ivAddShds.setVisibility(View.GONE);
        }
    }

    public interface Listener {
        void onUpload();

        void onCommit(List<String> url);
    }
}
